package com.example.add;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Handler;
import android.util.Log;

import com.example.add.ShakeListener.OnShakeListener;
import com.googlecode.androidannotations.annotations.AfterViews;
import com.googlecode.androidannotations.annotations.Bean;
import com.googlecode.androidannotations.annotations.EActivity;

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {

	@Bean(ShakeListener.class)
	ShakeListener mShakeListener;// 构造一个摇晃监听器
	FragmentManager fragmentManager;// FragMent管理者
	FragmentTransaction fragmentTransaction;// FragMent返回的接收器
	MyFragment myfragment = new MyFragment();// 创建获得MyFragMent对象

	@AfterViews
	public void setSharkeListenter() {
		mShakeListener.setOnShakeListener(new OnShakeListener() {

			@Override
			public void onShake() {
				mShakeListener.stop();// 停止检测
				new Handler().postDelayed(new Runnable() {

					@Override
					public void run() {
						fragmentManager = getFragmentManager();// 获得一个FragMent管理者
						fragmentTransaction = fragmentManager
								.beginTransaction();// 开始FragMent
						fragmentTransaction.add(R.id.myFragment, myfragment);// 添加FragMent注意
																				// 第一个参数是id型的
						fragmentTransaction.commit();// 提交FragMent
						Log.i("run", "go");
						System.out.println("aaaaaa");
						mShakeListener.start();// 摇晃重新监听
					}
				}, 2000);
			}
		});
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mShakeListener != null) {
			mShakeListener.stop();// 停止检测
		}
	}
}
